package com.ls.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.domain.Student;
import com.ls.service.StudentService;
import com.ls.service.TeacherService;
import com.ls.utils.StudentExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    //登录
    @PostMapping("/login")
    public Student login(HttpServletRequest request, @RequestBody Student student) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getSno, student.getSno())
                .eq(Student::getStudentPassword, student.getStudentPassword());
        Student stu = studentService.getOne(queryWrapper);
        request.getSession().setAttribute("student", stu.getId());
        return stu;
    }

    //登出
    @PostMapping("/logout")
    public boolean logout(HttpServletRequest request) {
        request.getSession().removeAttribute("student");
        return true;
    }

    //增加学生
    @PostMapping("/save")
    public boolean save(@RequestBody Student student) {
        return studentService.save(student);
    }

    //通过文件批量增加学生
    @PostMapping("/excelExport")
    public void saves(@RequestParam(value = "file", required = false) MultipartFile file) {
        List<Student> excelInfo = StudentExcelUtils.getExcelInfo(file);
        for (Student patientInfo : excelInfo) {

            System.err.println(patientInfo);
            System.err.println("==========");

            String sno = patientInfo.getSno();
            String studentName = patientInfo.getStudentName();
            String studentClass = patientInfo.getStudentClass();
            String studentSex = patientInfo.getStudentSex();

            Student student = new Student();

            student.setSno(sno);
            student.setStudentName(studentName);
            student.setStudentClass(studentClass);
            student.setStudentSex(studentSex);

            studentService.save(student);
        }

    }

    //根据id删除学生
    @DeleteMapping("/deleteById/{id}")
    public boolean delete(@PathVariable("id") Integer id) {
        return studentService.removeById(id);
    }

    //根据ids批量删除学生
    @PostMapping("/deleteByIds")
    public boolean deletes(@RequestBody List<Integer> ids) {
        return studentService.removeByIds(ids);
    }

    //学生改密码
    @PutMapping("/password/{id}")
    public boolean password(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    //重置学生密码
    @GetMapping("/resetPassword/{id}")
    public boolean resetPassword(@PathVariable("id") Integer id) {
        Student student = studentService.getById(id);
        student.setStudentPassword("123456");
        return studentService.updateById(student);
    }

    //根据id和学生实体改学生信息
    @PutMapping("/update/{id}")
    public boolean update(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    //根据id查学生
    @GetMapping("/selectById/{id}")
    public Student selectById(@PathVariable("id") Integer id) {
        return studentService.getById(id);
    }

    //管理员查学生
    @GetMapping("/selectAll")
    public Map<String, Object> selectAll(int currentPage, int pageSize) {
        Page<Student> pageInfo = new Page<>(currentPage, pageSize);
        studentService.page(pageInfo);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }

    //管理员搜索学生 学号、姓名、班级
    @PostMapping("/selectByConditions")
    public Map<String, Object> selectByConditions(@RequestBody Student student, @RequestParam("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Student::getSno, student.getSno())
                .like(Student::getStudentName, student.getStudentName())
                .like(Student::getStudentClass, student.getStudentClass());
        Page<Student> pageInfo = new Page<>(currentPage, pageSize);
        studentService.page(pageInfo, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }

    //教师查学生
    @GetMapping("/selectAllTch")
    public Map<String, Object> selectAllTch(int currentPage, int pageSize, String teacherClass) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getStudentClass, teacherClass);
        Page<Student> pageInfo = new Page<>(currentPage, pageSize);
        studentService.page(pageInfo,queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }

    //教师搜索学生 学号、姓名
    @PostMapping("/selectByConditionsTch")
    public Map<String, Object> selectByConditionsTch(@RequestBody Student student, @RequestParam("teacherClass") String teacherClass, @RequestParam("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Student::getSno, student.getSno())
                .like(Student::getStudentName, student.getStudentName())
                .eq(Student::getStudentClass, teacherClass);
        Page<Student> pageInfo = new Page<>(currentPage, pageSize);
        studentService.page(pageInfo, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("tableData", pageInfo.getRecords());
        map.put("totalCount", pageInfo.getTotal());
        return map;
    }
}
